package com.yanfan.energy.controller;

import cn.hutool.core.date.DateUtil;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.energy.domain.EnergyCopDTO;
import com.yanfan.energy.service.EnergyCopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cop/api/")
public class CopController {

    @Autowired
    private EnergyCopService energyCopService;


    @GetMapping("/calcCop")
    public AjaxResult calcCop() throws Exception {
        // 168,169,183,184,191
        String beginDate = DateUtil.beginOfDay(DateUtil.yesterday()).toString();
        String endDate = DateUtil.beginOfDay(new Date()).toString();
        List<EnergyCopDTO> result = energyCopService.calcCop(beginDate, endDate);
        String todayDate = DateUtil.format(DateUtil.beginOfDay(DateUtil.yesterday()), "yyyy-MM-dd ");

        Map<String, EnergyCopDTO> collect = result
                .stream()
                .collect(Collectors.toMap((item) -> item.getCreatedDate() + item.getDeviceId(), Function.identity()));
        for (int i = 0; i < 24; i++) {
            String formatDate = "";
            if (i < 10) {
                formatDate = todayDate + "0" + i;
            } else {
                formatDate = todayDate + i;
            }
            EnergyCopDTO _168CopDTO = collect.get(formatDate + "168");
            EnergyCopDTO _169CopDTO = collect.get(formatDate + "169");
            EnergyCopDTO _183CopDTO = collect.get(formatDate + "183");
            EnergyCopDTO _184CopDTO = collect.get(formatDate + "184");
            EnergyCopDTO _191CopDTO = collect.get(formatDate + "191");
            double tmpHot = 0.0;
            if (_191CopDTO != null) {
                tmpHot = _191CopDTO.getTotalConsumption() * 1000;
            }
            double electTotal = 0.0;
            double cop = 0;
            if (_168CopDTO != null) {
                electTotal = _168CopDTO.getTotalConsumption() + electTotal;
            }
            if (_169CopDTO != null) {
                electTotal = _169CopDTO.getTotalConsumption() + electTotal;
            }
            if (_183CopDTO != null) {
                electTotal = _183CopDTO.getTotalConsumption() + electTotal;
            }
            if (_184CopDTO != null) {
                electTotal = _184CopDTO.getTotalConsumption() + electTotal;
            }
            if (electTotal != 0.0) {
                cop = tmpHot / (3.6 * electTotal);
            }
            EnergyCopDTO copDTO = new EnergyCopDTO();
            copDTO.setCop(cop);
            copDTO.setCreateDate(DateUtil.parse(formatDate, "yyyy-MM-dd HH"));
            energyCopService.insertCop(copDTO);
        }
        return AjaxResult.success();
    }

    @GetMapping("/getCop")
    public AjaxResult getCop(String beginDate, String endDate) throws Exception {

        List<EnergyCopDTO> copDTOS = energyCopService.selectCop(beginDate, endDate);

        return AjaxResult.success(copDTOS);
    }
}
